package com.rp.system.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.jfinal.plugin.activerecord.Db;
import com.rp.common.utils.JsonUtil;
import com.rp.common.vo.PageData;
import com.rp.controller.RpController;
import com.rp.system.po.Role;
import com.rp.system.service.RoleMenuService;
import com.rp.system.service.RoleService;
import com.rp.system.service.UserService;
import com.rp.system.vo.RoleVo;

@Controller
@RequestMapping("/rp/system/roleMng/")
public class RoleController extends RpController {

	@Autowired
	private UserService userService;
	@Autowired
	private RoleService roleService;
	@Autowired
	private RoleMenuService rmService;
	
	@RequestMapping("showRoleGrid")
	public ModelAndView showRoleGrid(){
		
		return forward("/rp/system/roleMng/roleGrid.jsp");
	}
	
	@ResponseBody
	@RequestMapping("getAllRolesByList")
	public List<Role> getAllRolesByList(){
		return roleService.getRoleDao().getRoles(new RoleVo());
	}
	
	@ResponseBody
	@RequestMapping("getRoles")
	public PageData getRoles(RoleVo roleVo){
		return new PageData(roleService.getRoleDao().getRolesByPage(pageParam, roleVo));
	}
	
	@ResponseBody
	@RequestMapping("getAllRoles")
	public PageData getAllRoles(){
		
		return new PageData(10000, roleService.getRoleDao().getAllRoles());
	}
	
	/**
	 * 修改了角色与用户的关系之后，用户与角色的关系不再是多对多关系，因此此方法已弃用
	 * @param userId
	 * @param target
	 * @param roleVo
	 * @return
	 */
	@Deprecated
	@ResponseBody
	@RequestMapping("getAllotRoles")
	public PageData getAllotRoles(String userId, String target, RoleVo roleVo){
		/*//当前登录用户是否有超级管理员角色
		boolean hasSuperRole = User.isSuperUser();
		
		User user = userService.getUserDao().get(userId);
		List<Role> hasRoles = user.getRoles();
		List<Role> allRoles = new ArrayList<Role>();
		if(hasSuperRole){
			allRoles = roleService.getRoleDao().getRolesForSuperRole(roleVo);
		}else{
			allRoles = roleService.getRoleDao().getRoles(roleVo);
		}
		for(int i=0; i<allRoles.size(); i++){
			if(hasRoles.contains(allRoles.get(i))){
				allRoles.remove(i);
				i--;
			}
		}
		if(target == null){
			return new PageData(10000, new ArrayList<Role>());
		}else if(target.equals("wait")){
			return new PageData(10000, allRoles);
		}else if(target.equals("has")){
			return new PageData(10000, hasRoles);
		}*/
		return null;
	}
	
	@ResponseBody
	@RequestMapping("saveRoles")
	public String saveRoles(String data){
		try {
			List<Role> roles = JsonUtil.jsonToList(data, "com.rp.system.po.Role");
			for (Role role : roles) {
				if(role.getId() == null){
					roleService.addRole(role.getName());
				}else{
					roleService.updateRole(role);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return SUCCESS;
	}
	
	@ResponseBody
	@RequestMapping("deleteRoles")
	public String deleteRoles(String data){
		try {
			List<Role> roles = JsonUtil.jsonToList(data, "com.rp.system.po.Role");
			for (Role role : roles) {
				roleService.deleteRole(role.getId());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	/**
	 * 显示分配菜单界面
	 * @param roleId
	 * @return
	 */
	@RequestMapping("showAllotMenus")
	public ModelAndView showAllotMenus(String roleId){
		model.put("roleId", roleId);
		return forward("/rp/system/roleMng/allotMenus.jsp");
	}
	
	/**
	 * 更新角色菜单
	 * @param menuItemIds 菜单项ID串 
	 * @return
	 */
	@ResponseBody
	@RequestMapping("updateRoleMenus")
	public String updateRoleMenus(String roleId, String menuId, String menuItemIds){
		
		rmService.updateRoleMenu(roleId, menuId, menuItemIds);
		
		return SUCCESS;
	}
	
	/**
	 * 获取角色拥有的菜单项ID串
	 * @param roleId
	 * @return
	 */
	@ResponseBody
	@RequestMapping("getRoleOwnMenuItems")
	public String getRoleOwnMenuItems(String roleId, String menuId){
		
		String menuItemIds = Db.use("rpDb").queryStr("select menuItemIds from RP_SYSTEM_ROLE_MENUS where roleId = ? and menuId = ?", roleId, menuId);
		
		return menuItemIds;
	}
	
}
